{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "4047ccaf-1b38-43ad-9737-0ec08deb860d",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "dotenv:  E:\\work\\code\\ai\\.env\n"
     ]
    }
   ],
   "source": [
    "import openai \n",
    "import utils_zh\n",
    "import os\n",
    "from openai import OpenAI\n",
    "from dotenv import load_dotenv, find_dotenv\n",
    "\n",
    "def get_openai_key():\n",
    "    print(\"dotenv: \",find_dotenv())\n",
    "    _ = load_dotenv(find_dotenv())\n",
    "    # api_key = os.environ['OPENAI_API_KEY']\n",
    "    return OpenAI(\n",
    "        api_key = os.environ.get(\"ARK_API_KEY\"),\n",
    "    base_url = \"https://ark.cn-beijing.volces.com/api/v3\",)\n",
    "\n",
    "client = get_openai_key()\n",
    "\n",
    "def get_completion_from_messages(message, model=\"ep-20250103110319-28xth\",temperature = 0, max_tokens=500):\n",
    "    # message = [\n",
    "    #     {\"role\": \"user\", \"content\": prompt},\n",
    "    # ]\n",
    "    completion = client.chat.completions.create( \n",
    "    model = model,  # your model endpoint ID\n",
    "    messages =message,\n",
    "    temperature=temperature,\n",
    "    max_tokens=max_tokens\n",
    "    )\n",
    "    return completion.choices[0].message.content\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "7e1e782c-bcfe-4bb7-a6f0-0ddd1e90d2ab",
   "metadata": {},
   "outputs": [],
   "source": [
    "'''\n",
    "注意：限于模型对中文理解能力较弱，中文 Prompt 可能会随机出现不成功，可以多次运行；也非常欢迎同学探究更稳定的中文 Prompt\n",
    "'''\n",
    "\n",
    "\n"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.21"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
